/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.cvut.fel.taskpro.data.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;



/**
 * This entity represents team. Each team has multiple students and is part of
 * study group.
 * @author Tom
 * @version 1.0.0 beta
 */
@Entity
@Table(name = "team")
@NamedQueries({
    @NamedQuery(name = "Team.FindByKantor", query = "SELECT t FROM Team t WHERE t.group.kantor = :group"),
     @NamedQuery(name = "Team.FindByGroup", query = "SELECT t FROM Team t WHERE t.group = :group") 
})
public class Team implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    @Column(name = "name", nullable = false)
    private String name;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private GroupTaskPro group;
    @ManyToMany(mappedBy="team",fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    private List<Student> mStudents;
    @ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    @OrderBy("dateOfAssignment ASC")
    private List<Assignment> mAssignments;
    @OneToMany(mappedBy="team",fetch = FetchType.LAZY, cascade = {CascadeType.REFRESH, CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST})
    List<AssignmentSubmit> mAssignmentSubmits;
    public void setmStudents(List<Student> mStudents) {
        this.mStudents=mStudents;
    }

    public void setmAssignments(List<Assignment> mAssignments) {
        this.mAssignments= mAssignments;
    }

    /**
     * prida studenta do skupiny
     * @param s - novy student
     */
    public void addStudent(Student s) {
        if (mStudents == null) {
            mStudents = new ArrayList<Student>();
        }
        mStudents.add(s);
    }
    public void addAssignment(Assignment a){
        if(mAssignments==null){
            mAssignments=new ArrayList<Assignment>();
        }
        mAssignments.add(a);
    }
    public void addGroup(GroupTaskPro g){
        group=g;
        g.addTeam(this);
    }

    /**
     * prida novy ukol skupine
     * @param u - novy ukol
     */
    public void addUkol(Assignment u) {
        if (mAssignments== null) {
            mAssignments= new ArrayList<Assignment>();
        }
        mAssignments.add(u);
    }

    public List<Assignment> getmAssignments() {
        return mAssignments;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGroup(GroupTaskPro group) {
        this.group = group;
    }

    public List<Student> getmStudents() {
        return mStudents;
    }

    public String getName() {
        return name;
    }

    public GroupTaskPro getGroup() {
        return group;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) id;
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Team)) {
            return false;
        }
        Team other = (Team) object;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }
    public boolean equal(Object object){
        if (!(object instanceof Team)) {
            return false;
        }
        Team other = (Team) object;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    public List<AssignmentSubmit> getmAssignmentSubmits() {
        return mAssignmentSubmits;
    }

    public void setmAssignmentSubmits(List<AssignmentSubmit> mAssignmentSubmits) {
        this.mAssignmentSubmits = mAssignmentSubmits;
    }

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "DL.entity.Skupina[ id=" + id + " nazev=" + name + " ]";
    }
    public void addAssignmentSubmit(AssignmentSubmit assignmentSubmit){
        if(mAssignmentSubmits==null){
            mAssignmentSubmits=new ArrayList<AssignmentSubmit>();
        }
        mAssignmentSubmits.add(assignmentSubmit);
    }
    public void removeAssignment(Assignment assignmentToRemove){
        if(mAssignments==null){
            return;
        }
     mAssignmentSubmits.remove(assignmentToRemove);
    }
    
}
